package com.yan.controller;

import com.jfinal.core.Controller;
import com.yan.annon.RouterMapping;
import com.yan.bean.Diarytype;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * Created by yanl_ on 2018/1/14.
 */
@RouterMapping(url = "/diaryType", viewPath = "/")
public class DiarytypeController extends Controller {

    public void querydiaryTypeList() {

        List<Diarytype> diaryTypeList = Diarytype.dao.diaryTypeList();
//        setAttr("diaryTypeList", diaryTypeList);
//        setAttr("mainPage", "diaryType/diaryTypeList.jsp");

        setSessionAttr("diaryTypeList", diaryTypeList);
        setSessionAttr("mainPage", "diaryType/diaryTypeList.jsp");
        redirect("/main.jsp");

    }


    public void deleteDiaryTypeId() {
        String diaryTypeId = getPara("diaryTypeId");
        Diarytype.dao.deletediaryTypeBydiaryTypeId(Integer.valueOf(diaryTypeId));
        querydiaryTypeList();
    }


    public void editDiaryType() {
        String diaryTypeId = getPara("diaryTypeId");

        if (!StringUtils.isEmpty(diaryTypeId)) {
            Diarytype diaryType = Diarytype.dao.querydiaryTypeId(Integer.valueOf(diaryTypeId));
            setSessionAttr("diaryType", diaryType);
        }

        setSessionAttr("mainPage", "diaryType/diaryTypeSave.jsp");
        redirect("/main.jsp");
    }

    public void saveDiaryType() {

        Diarytype diaryType = getBean(Diarytype.class, "");

        boolean flag;
        if (diaryType.getDiaryTypeId() == null) {
            flag = Diarytype.dao.adddiaryType(diaryType);
        } else {

            flag = Diarytype.dao.updateDiaryType(diaryType);
        }
        if (flag) {
            querydiaryTypeList();
        } else {
            setSessionAttr("diaryType", diaryType);
            setSessionAttr("error", "操作失败");
            setSessionAttr("mainPage", "diaryType/diaryTypeSave.jsp");
            redirect("/main.jsp");
        }

    }

}
